<template>
	<el-card class="form-container">
		<el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()" label-width="90px">
			<el-form-item label="广告名称" prop="advertName">
				<el-input v-model="dataForm.advertName" placeholder="请输入广告名称"></el-input>
			</el-form-item>
			<el-form-item label="投放位置" prop="advertLaunchPosition">
				<el-radio-group v-model="dataForm.advertLaunchPosition">
					<el-radio label="1">PC首页轮播图</el-radio>
					<el-radio label="2">APP首页轮播图</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="投放类型" prop="advertLaunchType">
				<el-radio-group v-model="dataForm.advertLaunchType">
					<el-radio label="1">PC(外链)</el-radio>
					<el-radio label="2">APP(内链)</el-radio>
					<el-radio label="3">H5(外链)</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="广告类型" prop="advertType">
				<el-radio-group v-model="dataForm.advertType">
					<el-radio label="1">活动</el-radio>
					<el-radio label="2">商品</el-radio>
					<el-radio label="3">商铺</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="开始时间" prop="advertLaunchStartTime">
				<el-date-picker
					v-model="dataForm.advertLaunchStartTime"
					type="datetime"
					placeholder="选择开始时间"
					default-time="12:00:00">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="结束时间" prop="advertLanuchEndTime">
				<el-date-picker
					v-model="dataForm.advertLanuchEndTime"
					type="datetime"
					placeholder="选择结束时间"
					default-time="12:00:00">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="图片地址" prop="advertImage">
				<el-upload
					class="avatar-uploader"
					action="#"
					:show-file-list="false"
					:on-success="handleAvatarSuccess"
					:before-upload="beforeAvatarUpload"
					:auto-upload="true"
					:data="extraParameters"
					:http-request="uploadImageLogo">
					<img v-if="dataForm.advertImage" :src="dataForm.advertImage" class="avatar">
					<i v-else class="el-icon-plus avatar-uploader-icon"></i>
				</el-upload>
			</el-form-item>
			<el-form-item label="链接地址" prop="advertUrl">
				<el-input v-model="dataForm.advertUrl" placeholder="请输入广告链接地址"></el-input>
			</el-form-item>
			<el-form-item label="广告描述" prop="advertDesc">
				<el-input type="textarea" v-model="dataForm.advertDesc" placeholder="请输入广告描述"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="submit('dataForm')">确定</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
	import { addAdvertLaunch } from '@/api/activity/advertLaunch'
	import { uploadImage } from '@/api/common/upload'
	export default {
		name: 'add-advertLaunch',
		data() {
			return {
				dataForm: {
					advertName: '',
					advertLaunchType: '',
					advertLaunchPosition: '',
					advertUrl: '',
					advertImage: '',
					advertType: '',
					advertLaunchStartTime: '',
					advertLanuchEndTime: '',
					advertDesc: '',
				},
				dataRule: {
					advertName: [{
							required: true,
							message: '请输入广告名称',
							trigger: 'blur'
						},
					],
					advertLaunchType: [{
							required: true,
							message: '请输入广告投放类型',
							trigger: 'blur'
						},
					],
					advertLaunchPosition: [{
							required: true,
							message: '请输入广告投放位置',
							trigger: 'blur'
						},
					],
					advertUrl: [{
							required: true,
							message: '请输入广告链接地址',
							trigger: 'blur'
						},
					],
					advertImage: [{
							required: true,
							message: '请输入广告图片地址',
							trigger: 'blur'
						},
					],
					advertType: [{
							required: true,
							message: '请输入广告类型',
							trigger: 'blur'
						},
					],
					advertLaunchStartTime: [{
							required: true,
							message: '请输入广告投放开始时间',
							trigger: 'blur'
						},
					],
					advertLanuchEndTime: [{
							required: true,
							message: '请输入广告投放结束时间',
							trigger: 'blur'
						},
					],
					advertDesc: [{
							required: true,
							message: '请输入广告描述',
							trigger: 'blur'
						},
					],
				},
				extraParameters:{
					uploadType:'eyeglass-user'
				},
			}
		},
		methods: {
			cancel() {
				this.$router.go(-1)
			},
			submit() {
				const set = this.$refs;
				set['dataForm'].validate((valid) => {
					if (!valid) {
						return false
					}
					addAdvertLaunch(this.dataForm).then(() => {
						this.$notify({
							title: '添加成功',
							message: '添加成功',
							type: 'success',
							duration: 2000,
							onClose: this.$router.go(-1)
						})
					})
				})
			},
			beforeAvatarUpload(file) {
				const isJPG = file.type === 'image/jpeg';
				const isLt2M = file.size / 1024 / 1024 < 2;
				if (!isJPG) {
					this.$message.error('上传头像图片只能是 JPG 格式!');
				}
				if (!isLt2M) {
					this.$message.error('上传头像图片大小不能超过 2MB!');
				}
				return isJPG && isLt2M;
			},
			handleAvatarSuccess(res, file) {
				this.dataForm.advertImage = URL.createObjectURL(file.raw);
			},
			uploadImageLogo(val){
				let formData = new FormData();
				formData.append('uploadType',this.extraParameters.uploadType)
				formData.append('files',val.file)
				uploadImage(formData).then(res =>{
					this.dataForm.advertImage = res.data.responseData[0]
				})
			},
		}
	}
</script>

<style>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.avatar-uploader .el-upload:hover {
	border-color: #409EFF;
}
.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 160px;
	height: 160px;
	line-height: 160px;
	text-align: center;
}
.avatar {
	width: 160px;
	height: 160px;
	display: block;
}
</style>
